﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MSWord = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Core;
using System.IO;
using System.Windows.Forms;

namespace CodeCounsel.Office.DatabindingToolkit
{
    public partial class ThisAddIn
    {
        TaskPaneManager<TaskPane> _taskPaneManager = null;
        DocumentManager _documentManager = null;

        public DocumentManager DocumentManager
        {
            get
            {
                if (_documentManager == null)
                {
                    _documentManager = new DocumentManager();
                }
                return _documentManager;
            }
        }

        public TaskPaneManager<TaskPane> TaskPaneManager
        {
            get
            {
                if (_taskPaneManager == null)
                {
                    _taskPaneManager = new TaskPaneManager<TaskPane>(
                        new TaskPaneFactory());
                }
                return _taskPaneManager;
            }
        }

        public void ShowInsertCustomXmlPartDialog()
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect = false;
                dialog.Filter = "XML files|*.xml|All files|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    InsertCustomXmlPart(dialog.FileName);
                }
            }
        }

        void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        void InsertCustomXmlPart(string path)
        {
            CustomXMLPart customXmlPart = Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts.Add(
                File.ReadAllText(path), Type.Missing);
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion

        class TaskPaneFactory :
            TaskPaneManager<TaskPane>.ITaskPaneFactory
        {
            public string CreateTitle(TaskPane taskPane)
            {
                return "Databinding";
            }

            public TaskPane CreateNewTaskPane(MSWord.Document document, MSWord.Window window)
            {
                TaskPane taskPane = new TaskPane(
                    Globals.ThisAddIn.DocumentManager.CreateDocument(document));
                return taskPane;
            }
        }
    }
}
